package com.ahut.service.impl;

import com.ahut.pojo.vo.PortalVo;
import com.ahut.utils.JwtHelper;
import com.ahut.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ahut.pojo.Headline;
import com.ahut.service.HeadlineService;
import com.ahut.mapper.HeadlineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author zhuang05
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2025-03-07 20:51:41
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{


    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 首页数据查询
     * 1.进行分页查询
     * 2.分页数据，拼接到result即可
     *
     * 查询需要自定义语句，自定义mapper方法，携带分页‘
     * 返回的结果装在List<Map>
     * @param portalVo
     * @return
     */
    @Override
    public Result findNewsPage(PortalVo portalVo) {

        IPage<Map> page = new Page<>(portalVo.getPageNum(),portalVo.getPageSize());
        headlineMapper.selectMyPage(page,portalVo);

        List<Map> records = page.getRecords();
        Map data = new HashMap<>();
        data.put("pageData",records);
        data.put("pageNum",page.getCurrent());
        data.put("pageSize",page.getSize());
        data.put("totalPage",page.getPages());
        data.put("totalSize",page.getTotal());
        Map pageInfo = new HashMap<>();
        pageInfo.put("pageInfo",data);
        return Result.ok(pageInfo);
    }


    /**
     * 展示头条具体内容，需要将浏览量+1
     * @param hid
     * @return
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {
        //根据头条hid获得头条信息
        Map data = headlineMapper.queryDetailMap(hid);

        //查询浏览量
        //根据uid将浏览量+1
        Headline updateHeadline = new Headline();
        updateHeadline.setHid(hid);
        updateHeadline.setPageViews((Integer) data.get("pageViews") + 1);
        //更新版本
        updateHeadline.setVersion((Integer) data.get("version"));
        //更改数据库
        int rows = headlineMapper.updateById(updateHeadline);

        //更新浏览量
        data = headlineMapper.queryDetailMap(hid);

        Map headline = new HashMap<>();
        headline.put("headline", data);
        return Result.ok(headline);
    }


    /**
     * 1.解析token获得当前用户信息
     * 2.将用户信息和头条信息存入数据库
     * @param headline
     * @param token
     * @return
     */
    @Override
    public Result publish(Headline headline, String token) {
        //获得用户id
        Long userId = jwtHelper.getUserId(token);

        //补全信息
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());

        //插入数据库
        int insert = headlineMapper.insert(headline);
        return Result.ok(null);
    }

    /**
     * 展示头条详细信息
     * @param hid
     * @return
     */
    @Override
    public Result findHeadlineByHid(Integer hid) {
        //查询所需信息
        Headline headline = headlineMapper.selectById(hid);

        Map headlineMap = new HashMap<>();
        headlineMap.put("headline", headline);
        return Result.ok(headlineMap);
    }

    /**
     * 更新头条信息
     * @param headline
     * @return
     */
    @Override
    public Result updateHeadline(Headline headline) {
        //获取版本
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();

        headline.setVersion(version);
        headline.setUpdateTime(new Date());

        headlineMapper.updateById(headline);
        return Result.ok(null);
    }

    /**
     * 根据hid删除头条信息
     * @param hid
     * @return
     */
    @Override
    public Result removeByHid(String hid) {
        headlineMapper.deleteById(hid);
        return Result.ok(null);
    }
}




